/**
 * 次均医疗费用
 * @Title: indicators_avgCost_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author ex-manyansong001
 * @date 2014年10月15日
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	
	//次均医疗费用
	app.controller('KPI0014Ctrl',['$scope','SettleAnalyseChartService','SettleAnalyseService',
	                                function($scope,SettleAnalyseChartService,SettleAnalyseService){
		
		/*********************************************************
		 * 
		 * Scope & Variables Init
		 * 
		 */
		
		var colors = ['green','#dd4814',  '#FF855A','#006397','#3ca5d0','#86d3e4','yellow'];
		var colors2 = ['#dd4814','#3ca5d0','#006397','#86d3e4','#aaa'];
		$scope.$watch('$parent.query.data',function(v){
			if(v){
				$scope.lineOption.xAxis= [{							
	             type : 'category',
	             boundaryGap:false,
	             data : v				        
				}];
				$scope.barOption.xAxis = [{
		             type : 'category',
		             data : v
		         }];
			}
		});
		var lineSeries = [],
			barSeries = [];
		
		$scope.barOption = {
			grid:{
				y:10,
				y2:25,
				x:40,
				x2:50
			},
			series:[],
			xAxis : [{
	             type : 'category',
	             data : $scope.$parent.query.data
	         }],
	         yAxis : [
                  {
                      type : 'value',
                      axisLabel : {
                    	  formatter: '{value}%'
                      },
                      splitArea : {show : false}
                  },
                  {
                      type : 'value',
                      axisLabel : {
                    	  formatter: '{value}万'
                      },
                      splitArea : {show : false},
                      axisLine : {show : false} //y轴轴线
                  }
              ],
	         color:colors
		};
		
		$scope.lineOption = {
				grid:{
					y:10,
					y2:25,
					x:55
				},
			xAxis : [{
	             type : 'category',
	             boundaryGap:false,
	             data : $scope.$parent.query.data
	         }],
	         yAxis : [
			        {
			            type : 'value',
			            axisLabel : {
			            	formatter: '{value}万'
			            }
			        }
			  ],
	         series:[],
	         color:colors2
		};
		
		
		/*********************************************************
		 * 
		 * Private function
		 * 
		 */
		
		function change2LegendData(arr, seriesArr) {
			var resArray = [];
			var ci = 0;
			$.each(arr, function(i, d){
				resArray.push({
					data:d,
					title:d.name,
					items:[{
						type:'bar',
						name:'基金支付占总费用比例',
						color:colors[ci],
						series:seriesArr[ci++]
					},{
						type:'bar',
						name:'统筹基金支付',
						color:colors[ci],
						series:seriesArr[ci++]
					}]
				});
			});
			
			return resArray;
		}
		
		function change2LegendData2(arr, seriesArr) {
			var resArray = [];
			var ci = 0;
			$.each(arr, function(i, d){
				resArray.push({
					data:d,
					title:d.name,
					items:[{
						name:'医疗费用总额',
						type:'line',
						color:colors2[ci++],
						series:seriesArr[i]
					}]
				});
			});
			
			return resArray;
		}
		
		function checkedChange(arr) {
			var ci = 0, cj = 0;
			var kpiId = "KPI0014";
			SettleAnalyseChartService.getKpiChartValue(arr,kpiId,$scope.$parent.query.date).
				success(function(res){
					lineSeries = [];
					barSeries = [];
					if (res && res.data) {
						$.each(res.data, function(){
							lineSeries.push({
								type:'line',
								itemStyle: {
									normal: {
										color:colors2[cj++]
									}
								},
								name:this.name + ":医疗费用总额",//要与图例名称保持一致
								data:this.KPI0001
							});
							
							barSeries.push({
								type:'bar',
								name:this.name +':基金支付占总费用比例',
								stack:this.name,
								yAxisIndex: 0,//左侧y轴
								data:this.KPI0014,
								itemStyle: {normal: {color: colors[ci++]}}
							});
							barSeries.push({
								type:'line',
								name:this.name +':统筹基金支付',
								stack:this.name,
								yAxisIndex: 1,//右侧y轴
								data:this.KPI0023,
								itemStyle: {normal: {color: colors[ci++]}}
							});
						});
					}
					$scope.lineOption.series = lineSeries;
					$scope.barOption.series = barSeries;
					
					$scope.checkDatas = change2LegendData(arr, barSeries);
					$scope.checkDatas2 = change2LegendData2(arr, lineSeries);
					//关闭遮罩层
					SettleAnalyseService.loading = false;
				});
		}
		
		/*********************************************************
		 * 
		 * Event Handler
		 * 
		 */
		
		$scope.$on('fwaLegendGroup.legendStateChange', function(e, legendItem){
			
			var seriesArray, idx;
			if(legendItem.type === 'line') {
				seriesArray = $scope.lineOption.series;
			} else {
				seriesArray = $scope.barOption.series;
			}
			
			if(legendItem.hide) {
				idx = seriesArray.indexOf(legendItem.series);
				if(idx != -1) {
					seriesArray.splice(idx,1);
				} 
			} else {
				seriesArray.push(legendItem.series);
			}
		});
		
		$scope.$on('fwaLegendGroup.close',function(e, data){
			var data = SettleAnalyseService.removeFromCheckedArray({id:data.data.id});
			
			data.checked = false;
			
			checkedChange(SettleAnalyseService.checkedArray);
		});
		
		$scope.$on('settleAnalyse.dataTypeChange', function(e, type){
			checkedChange(SettleAnalyseService.checkedArray);
		});
		
		$scope.$on('settleAnalyse.rowCheckChange', function(e, arr){
			checkedChange(arr);
		});
		
	}]);
	
})();